package com.vecspace.database.model;

import org.apache.commons.lang3.StringUtils;

/**
 * 用于对比两个DataRow是否可以连接的数据（一般为主外键字段值，也可以是多个字段）
 * @author wanghua
 *
 */
public class JoinData {
	private int hashcode = 0;
	private String[] joinData;
	public JoinData(int dataCount){
		joinData = new String[dataCount];
	}
	
	public JoinData(String[] joinData){
		this.joinData = new String[joinData.length];
		setJoinData(joinData);
	}
	
	public void setJoinData(int index, String value){
		joinData[index] = value;
	}
	
	public void setJoinData(String[] joinData){
		for(int i=0; i<joinData.length; ++ i){
			this.joinData[i] = joinData[i];
		}
	}
	
	public String getJoinData(int index){
		return joinData[index];
	}
	
	public String[] getJoinData(){
		return joinData;
	}
	
	
	public int getJoinDataCount(){
		return joinData.length;
	}
	
	private DataRow dataRow;
	
	public DataRow getDataRow() {
		return dataRow;
	}

	public void setDataRow(DataRow dataRow) {
		this.dataRow = dataRow;
	}

	/**
	 * 所有值都不为空，并且都相等，才返回true
	 * @return 
	 */
	@Override	 
	public boolean equals(Object o){
		if(!(o instanceof JoinData)) {
			return false;
		}
		JoinData other = (JoinData)o;
		if(this.getJoinDataCount() == 0) {
			return false;
		}
		if(this.getJoinDataCount() != other.getJoinDataCount()) {
			return false;
		}
		int dataCount = this.getJoinDataCount();
		boolean eq = true;
		for(int i=0; i<dataCount; ++i){ //所有值都不为空，并且都相等，才返回true
			String data1 = this.joinData[i];
			String data2 = other.joinData[i];
			if(data1 == null || data1.trim().length()==0) {
				eq = false; //出现空值，则认为不相等
			}
			else if(!data1.equals(data2)) {
				eq = false;
			}
			if(eq == false) {
				break;
			}
		}
		return eq;
	}
	
	@Override
	public int hashCode(){
		if(hashcode == 0) {
			hashcode = StringUtils.join(this.joinData,"##").hashCode();
		}
		return hashcode;
	}
}
